<?php
namespace app\wechat\controller;


use think\View;
use think\Db;
use Jssdk\JSSDK;

class RAustin extends Common
{


    // 售后列表
    public function index() {
        return view();
    }

    // 服务单售后列表
    public function get_server_list() {

        /*PJ非微信本地测试方法*/
        if( @intval($_COOKIE["PJ"]) ){session('member_id',@intval($_COOKIE["PJ"]));}

        $param = [
            'member_id' => session('member_id'),
            'type' => 2
        ];
        $data = call_interface('repair_austin_list',$param);
        //dump($data);
        $view = new View();
        //dump($data);
        if ($data['code'] == 200) {
            $view->assign('data', $data['data']['data']);
        } else {
            $view->assign('data', []);
        }
        $tpl = $view->fetch('austin/list_tpl');
        $data['tpl'] = $tpl;
        return json($data);
    }

    // 售后单详情
    public function austin_info() {

        /*PJ非微信本地测试方法*/
        if( @intval($_COOKIE["PJ"]) ){
            session('member_id',@intval($_COOKIE["PJ"]));
            $signPackage = [
                'appId' => '',
                'timestamp' => '',
                'nonceStr' => '',
                'signature' => ''
            ];
        }else{
            $jssdk = new JSSDK(config('wechat.appid'), config('wechat.appsecret'));
            $signPackage = $jssdk->GetSignPackage();
        }
        $this->assign('signPackage', $signPackage);

        $id = input('param.id');

        $data = db('order_austin')->where('id', $id)->find();

        //pj170329add多图弄成数组
        if(!is_weixin()){
            $data["img"] = array();
            if($data["img1"]){$data["img"][] = $data["img1"];}
            if($data["img2"]){$data["img"][] = $data["img2"];}
            if($data["img3"]){$data["img"][] = $data["img3"];}
            //unset($data["img1"],$data["img2"],$data["img3"]);
        }

        $this->assign('data', $data);
        return view();
    }

    // 申诉页面
    public function complaint() {
        $id = input('param.id');
        $data = db('order_austin')->where('id', $id)->find();
        $this->assign('data', $data);
        $models = ['', 'order_buy', 'order_server'];
        $model = $models[$data['type']];
        $order = db($model)->field('order_sn')->where('id', $data['order_id'])->find();
        $this->assign('order', $order);
        return view();
    }

    // post 申诉提交
    public function save_complaint() {
        $id = input('param.id');
        $param = [
            'audit_result_msg' => input('param.audit_result_msg'),
            'audit_result_cause' => input('param.audit_result_cause'),
            'status' => 4,
            'audit_result_time' => time()
        ];
        try {
            db('order_austin')->where('id', $id)->update($param);
            return json(['coe' => 200, 'msg' => 'success']);
        } catch (\Exception $e) {
            return json(['coe' => 0, 'msg' => $e->getMessage()]);
        }
    }

    // 申诉页面列表
    public function complaint_index() {
        return view();
    }

    // 获取申诉列表
    public function get_complaint_list() {
        $param = [
            'member_id' => session('member_id'),
            'type' => 2
        ];
        $data = call_interface('repair_complaint_list',$param);
        //dump($data);
        $view = new View();
        //dump($data);
        if ($data['code'] == 200) {
            $view->assign('data', $data['data']['data']);
        } else {
            $view->assign('data', []);
        }
        $tpl = $view->fetch('austin/complaint_list_tpl');
        $data['tpl'] = $tpl;
        return json($data);
    }

    // 申诉详情
    public function complaint_info() {

        /*PJ非微信本地测试方法*/
        if( @intval($_COOKIE["PJ"]) ){
            session('member_id',@intval($_COOKIE["PJ"]));
            $signPackage = [
                'appId' => '',
                'timestamp' => '',
                'nonceStr' => '',
                'signature' => ''
            ];
        }else{
            $jssdk = new JSSDK(config('wechat.appid'), config('wechat.appsecret'));
            $signPackage = $jssdk->GetSignPackage();
        }
        $this->assign('signPackage', $signPackage);

        $id = input('param.id');
        $data = db('order_austin')->where('id', $id)->find();

        //pj170329add多图弄成数组
        if(!is_weixin()){
            $data["img"] = array();
            if($data["img1"]){$data["img"][] = $data["img1"];}
            if($data["img2"]){$data["img"][] = $data["img2"];}
            if($data["img3"]){$data["img"][] = $data["img3"];}
            //unset($data["img1"],$data["img2"],$data["img3"]);
        }

        $this->assign('data', $data);
        return view();
    }

    // 退款维修店拒绝
    public function austin_repair_tongyi() {
        $id = input('param.id');
        Db::startTrans();
        try {

            $austin = Db::table('mob_order_austin')->alias('oa')->field('oa.*, r.member_id as r_member_id, r.repair_name, mr.nickname')
                ->join('mob_member_role_info mr', 'mr.member_id = oa.member_id and mr.type=0')
                ->join('mob_repair r', 'r.id = oa.obj_id')
                ->where('oa.id', $id)->find();

            Db::table('mob_order_austin')->where('id', $id)->update(['status' => 1]);
            if ($austin['audit_type'] == 1) {
                Db::table('mob_repair')->where('id', $austin['obj_id'])->setDec('money_frozen', $austin['return_money']);
                Db::table('mob_member')->where('id', $austin['member_id'])->setInc('money',$austin['return_money']);

                /* 添加账单: member_id(用户ID) , order_price(账单金额) , frozen_status(冻结状态) , type(用户角色) , msg(账单说明) , source(账单来源) , order_sn(订单号) , model(订单模型) , pay_type(支付方式) */
                //售后退款扣除维修店冻结金额
                $param = [
                    "member_id" => $austin['r_member_id'],
                    "order_price" => -$austin['return_money'],
                    "frozen_status" => 1,
                    "type" => 1,
                    "msg" => $austin['nickname'].' - 售后退款扣除',
                    "source" => 12,
                    "order_sn" => '',
                    "model" => 'order_server',
                    "pay_type" => '售后退款'
                ];
                call_interface('call_add_member_money',$param);
                //售后退款追加用户余额
                $param = [
                    "member_id" => $austin['member_id'],
                    "order_price" => $austin['return_money'],
                    "frozen_status" => 0,
                    "type" => 0,
                    "msg" => $austin['repair_name'].' - 售后退款追加',
                    "source" => 12,
                    "order_sn" => '',
                    "model" => 'order_server',
                    "pay_type" => '售后退款'
                ];
                call_interface('call_add_member_money',$param);

                //如果该订单的 支付金额等于或大于退款总金额 那么订单状态为关闭
                $order_price_payment = Db::table('mob_order_server')->where('id',$austin["order_id"])->value('order_price_payment');
                $order_price_payment = $order_price_payment?$order_price_payment:0;
                $return_money_sum = Db::table('mob_order_austin')->where('order_id',$austin["order_id"])->where('type',2)->where('status',1)->sum('return_money');
                $return_money_sum = $return_money_sum?$return_money_sum:0;
                if($order_price_payment<=$return_money_sum){
                    $status9 = [
                        "status" => 9
                    ];
                    Db::table('mob_order_server')->where('id',$austin["order_id"])->update($status9);
                }

            }

            Db::commit();
            return json(['code' => 200, 'msg' => 'success']);

        }catch (\Exception $e) {
            Db::rollback();
            return json(['code' => 0, 'msg' => $e->getMessage()]);
        }
    }
    // 退款维修店拒绝
    public function austin_repair_jujue() {
        $id = input('param.id');
        $merchant_msg = input('param.merchant_msg');
        Db::startTrans();
        try {
            Db::table('mob_order_austin')->where('id', $id)->update(['status' => 2,'merchant_msg' => $merchant_msg]);
            Db::commit();
            return json(['code' => 200, 'msg' => 'success']);
        }catch (\Exception $e) {
            Db::rollback();
            return json(['code' => 0, 'msg' => $e->getMessage()]);
        }
    }

}
